package com.film.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.film.dao.FilmTypeDao;
import com.film.exception.MyException;
import com.film.po.FilmType;
import com.film.service.FilmTypeService;

/**
 * 电影类型
 * 
 * @author Administrator
 *
 */
@Service
public class FilmTypeServiceImpl implements FilmTypeService {

	// 类型的dao
	private FilmTypeDao filmTypeDao;

	@Autowired
	public void setFilmTypeDao(FilmTypeDao filmTypeDao) {
		this.filmTypeDao = filmTypeDao;
	}

	@Transactional
	@Override
	public FilmType add(FilmType filmType) {
		int result = filmTypeDao.insertFilmType(filmType);
		if (result != 1) {
			throw new MyException("添加类型失败，类型的名称为：" + filmType.getTypeName());
		}

		FilmType filmType2 = filmTypeDao.selectFilmTypeById(filmType.getTypeId());

		if (filmType2 == null) {
			throw new MyException("查询类型失败，类型的id为：" + filmType.getTypeId());
		}

		return filmType2;
	}

	@Transactional
	@Override
	public void deleteById(int id) {
		int result = filmTypeDao.deleteFilmTypeById(id);
		if (result != 1) {
			throw new MyException("删除类型失败，类型的id为：" + id);
		}
	}

	@Transactional
	@Override
	public FilmType update(FilmType filmType) {
		FilmType filmType2 = filmTypeDao.selectFilmTypeById(filmType.getTypeId());

		if (filmType2 == null) {
			throw new MyException("要更新的类型不存在，类型的id为：" + filmType.getTypeId());
		}

		int result = filmTypeDao.updateNonEmptyFilmTypeById(filmType);

		if (result != 1) {
			throw new MyException("更新类型失败，类型的id为：" + filmType.getTypeId());
		}

		FilmType filmType3 = filmTypeDao.selectFilmTypeById(filmType.getTypeId());

		return filmType3;
	}

	@Override
	public FilmType findById(int id) {
		FilmType filmType = filmTypeDao.selectFilmTypeById(id);

		if (filmType == null) {
			throw new MyException("要查找的类型不存在，类型的id为：" + id);
		}
		return filmType;
	}

	@Override
	public List<FilmType> getList() {
		List<FilmType> list = filmTypeDao.selectFilmType();
		if(list==null || list.size()<1){
			throw new MyException("查找类型失败");
		}
		return list;
	}

}
